var app = getApp();
var utils = require("../../utils/util.js");
Page({
  data: {
    //图片列表
    picList: [],
    //反馈类型
    type: 0,
    //输入文字
    content: ''
  },
  onLoad: function(options) {
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  //添加照片
  addPic: function() {
    var that = this;
    wx.chooseImage({
      count: 3 - that.data.picList.length,
      success: function(r) {
        var list = [];
        for (var i of r.tempFiles) {
          var pic = {
            path: i.path,
            src: '',
            sequence: 0,
            width: 0,
            height: 0
          }
          list.push(pic);
        }
        //给一个URL属性.
        that.setData({
          picList: that.data.picList.concat(list)
        })
      },
    })
  }, //删除照片
  dropPic: function(e) {
    var index = e.currentTarget.dataset.index;
    var picList = this.data.picList;
    picList.splice(index, 1);
    this.setData({
      picList: picList
    })
  },
  //选择类型
  selType: function(e) {
    var index = e.currentTarget.dataset.index;
    if (this.data.type == index) {
      return;
    }
    this.setData({
      type: index
    })
  },
  inputContent: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过500
    if (cursor > 500) {
      this.setData({
        content: value.substring(0, 500)
      })
    } else {
      this.setData({
        content: value
      })
    }
  },
  submit: function() {
    var type = this.data.type;
    var content = this.data.content;
    var picList = this.data.picList;
    if (content == '') {
      wx.showToast({
        title: '请描述您要反馈的问题',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.showLoading({
      title: '正在提交',
      mask: true
    })
    if (picList.length != 0) {
      this.getPolicy();
    } else {
      this.addSuggest();
    }
  },
  //获取policy
  getPolicy: function() {
    var that = this;
    /* 从后端获取 Policy用于上传文件到阿里云oss */
    wx.request({
      url: app.globalData.requestUrl + 'getUploadFileOssPolicy',
      method: 'GET',
      data: {
        userId: app.globalData.user.id,
        type: 1
      },
      success: function(r) {
        if (r.data.code == 200) {
          that.upload(r.data.data, that.data.picList, 0);
        } else {
          that.addSuggest();
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络连接断开,提交建议失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //上传图片
  upload: function(policyData, picList, index) {

    var that = this;
    //图片上传完毕
    if (!(index < picList.length)) {
      //发布
      that.addSuggest();
      return;
    }

    //生成名称
    var uuid = utils.getUUID();
    //获取类型
    var mimeType = utils.getMimeType(picList[index].path);
    var src = uuid + mimeType

    wx.uploadFile({
      url: policyData.host,
      filePath: picList[index].path,
      name: 'file',
      formData: {
        key: src,
        policy: policyData.policy,
        OSSAccessKeyId: policyData.accessid,
        success_action_status: '200',
        signature: policyData.signature
      },
      //成功要递归操作
      success: function(r) {
        //r.statusCode == 200
        if (r.statusCode == 200) {
          picList[index].src = policyData.host + src;
          picList[index].sequence = index + 1;
          //递归调用
          that.upload(policyData, picList, index + 1);
        } else {
          picList[index].src = null;
          picList[index].sequence = index + 1;
          //递归调用
          that.upload(policyData, picList, index + 1);
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络连接断开,提交建议失败',
          icon: 'none',
          duration: 2000
        })
      },
    })
  },
  addSuggest: function() {
    var type = this.data.type;
    var content = this.data.content;
    var picList = this.data.picList;
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'addSuggest',
      method: 'POST',
      data: {
        userId: app.globalData.user.id,
        type: type,
        content: content,
        resources: picList
      },
      success: function(r) {
        wx.hideLoading();
        if (r.data.code == 200) {
          wx.showToast({
            title: '提交建议成功',
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '提交建议失败',
            icon: 'none',
            duration: 2000
          })
        }
        setTimeout(function() {
          that.rollback();
        }, 2000)
      }
    })
  }
})